using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Data.SqlClient;

namespace RugbyClub.ViewModels
{
    public class CoachWindowViewModel : INotifyPropertyChanged
    {
        private string _playerName;
        private string _playerAge;
        private string _playerSkills;
        private string _matchDate;
        private string _matchLocation;

        public string PlayerName
        {
            get => _playerName;
            set
            {
                _playerName = value;
                OnPropertyChanged();
            }
        }

        public string PlayerAge
        {
            get => _playerAge;
            set
            {
                _playerAge = value;
                OnPropertyChanged();
            }
        }

        public string PlayerSkills
        {
            get => _playerSkills;
            set
            {
                _playerSkills = value;
                OnPropertyChanged();
            }
        }

        public string MatchDate
        {
            get => _matchDate;
            set
            {
                _matchDate = value;
                OnPropertyChanged();
            }
        }

        public string MatchLocation
        {
            get => _matchLocation;
            set
            {
                _matchLocation = value;
                OnPropertyChanged();
            }
        }

        public ICommand AddPlayerCommand { get; }
        public ICommand AddMatchCommand { get; }

        public CoachWindowViewModel()
        {
            AddPlayerCommand = new RelayCommand(AddPlayer);
            AddMatchCommand = new RelayCommand(AddMatch);
        }

        private void AddPlayer()
        {
            // Implement your SQL Server logic to add a player
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "INSERT INTO Players (Name, Age, Skills) VALUES (@Name, @Age, @Skills)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Name", PlayerName);
                    command.Parameters.AddWithValue("@Age", PlayerAge);
                    command.Parameters.AddWithValue("@Skills", PlayerSkills);
                    command.ExecuteNonQuery();
                }
            }
        }

        private void AddMatch()
        {
            // Implement your SQL Server logic to add a match
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "INSERT INTO Matches (Date, Location) VALUES (@Date, @Location)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Date", MatchDate);
                    command.Parameters.AddWithValue("@Location", MatchLocation);
                    command.ExecuteNonQuery();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}